package com.homedo.async.mysql.support.prepare;

import com.homedo.async.common.dto.Pagination;
import com.homedo.async.mysql.support.dataobject.Example;
import com.homedo.async.mysql.support.dml.DMLEnum;
import com.homedo.async.mysql.support.meta.EntityMetaData;
import com.homedo.async.mysql.support.prepare.delete.Record2Sql4Delete;
import com.homedo.async.mysql.support.prepare.insert.Record2Sql4Insert;
import com.homedo.async.mysql.support.prepare.select.Record2Sql4Select;
import com.homedo.async.mysql.support.prepare.update.Record2Sql4Update;
import lombok.Data;

/**
 * @author jgshun
 * @date 2020/3/5 1:56 PM
 */
@Data
public class PrepareObject2Sql<T> {

    private Object record;
    private EntityMetaData entityMetaData;
    private DMLEnum dml;
    private PrepareObjectType prepareObjectType;
    private boolean includeNullVals;//是否包含空值

    private Pagination<T> pagination;


    public PrepareObject2Sql() {
    }

    public PrepareObject2Sql(T record, EntityMetaData<T> entityMetaData, DMLEnum dml, PrepareObjectType prepareObjectType, boolean includeNullVals) {
        this.record = record;
        this.entityMetaData = entityMetaData;
        this.dml = dml;
        this.prepareObjectType = prepareObjectType;
        this.includeNullVals = includeNullVals;
    }

    public Object[] prepare() {
        if (this.dml == DMLEnum.SELECT && this.prepareObjectType == PrepareObjectType.ENTITY_RECORD) {
            return Record2Sql4Select.prepare((T) this.record, this.entityMetaData, this.includeNullVals, this.pagination);
        } else if (this.dml == DMLEnum.DELETE && this.prepareObjectType == PrepareObjectType.ENTITY_RECORD) {
            return Record2Sql4Delete.prepare(this.record, this.entityMetaData, this.includeNullVals);
        } else if (this.dml == DMLEnum.INSERT && this.prepareObjectType == PrepareObjectType.ENTITY_RECORD) {
            return Record2Sql4Insert.prepare(this.record, this.entityMetaData, this.includeNullVals);
        } else if (this.dml == DMLEnum.UPDATE && this.prepareObjectType == PrepareObjectType.ENTITY_RECORD) {
            return Record2Sql4Update.prepare(this.record, this.entityMetaData, this.includeNullVals);
        } else if (this.prepareObjectType == PrepareObjectType.ENTITY_EXAMPLE) {
            return Example2Sql.prepare((Example) this.record, this.pagination);
        } else {
            throw new RuntimeException("[未知操作]");
        }
    }

}

